/**
 * @requires jquery.validate.js
 * @author 张慧华 z@j-ui.com
 */
(function($){
	if ($.validator) {
		$.validator.addMethod("alphanumeric", function(value, element) {
			return this.optional(element) || /^\w+$/i.test(value);
		}, "Letters, numbers or underscores only please");
		
		$.validator.addMethod("lettersonly", function(value, element) {
			return this.optional(element) || /^[a-z]+$/i.test(value);
		}, "Letters only please"); 
		
		$.validator.addMethod("phone", function(value, element) {
			return this.optional(element) || /^[0-9 \(\)]{7,30}$/.test(value);
		}, "Please specify a valid phone number");
		$.validator.addMethod("mobile", function(value, element) {
			return this.optional(element) || /^[0-9 \(\)]{11}$/.test(value);
		}, "Please specify a valid phone number");
		$.validator.addMethod("szztphone", function(value, element) {
			return this.optional(element) || /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(value);
		}, "Please specify a valid phone number");
		
		//20181017 惠国磊  以下 增加验证
		$.validator.addMethod("telePhone", function(value, element) {
			return this.optional(element) || /^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$/.test(value);
		}, "请输入正确的电话号码");
		
		$.validator.addMethod("postcode", function(value, element) {
			return this.optional(element) || /^[0-8][0-7]\d{4}$/.test(value);
		}, "请输入正确的邮政编码");
		
		$.validator.addMethod("email", function(value, element) {
			return this.optional(element) || /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/.test(value);
		}, "请输入正确的邮箱地址");
		
		/*20180809 戴阳 以下4行*/
		//中文姓名：
		$.validator.addMethod("chinaName", function(value, element) {
			return this.optional(element) || /^[\u4E00-\u9FA5]{1,6}$/.test(value);
		}, "中文姓名长度不超过6");
		
		/*20180116 惠国磊 以下4行*/
		//验证市民卡号：12位数字+字母
		$.validator.addMethod("cardNo_valid", function(value, element) {
			return this.optional(element) || /^[0-9]{12}$/.test(value);
		}, "市民卡号信息不正确");
		
	/*	20180201 惠国磊 以下4行
		//验证金额的输入：两位小数，三位三位隔开
		$.validator.addMethod("money_valid", function(value, element) {
			return this.optional(element) || /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/.test(value);
		}, "金额输入有误！");*/
		
		$.validator.addMethod("date", function(value, element) {
			value = value.replace(/\s+/g, "");
			if (String.prototype.parseDate){
				var $input = $(element);
				var pattern = $input.attr('dateFmt') || 'yyyy-MM-dd';
	
				return !$input.val() || $input.val().parseDate(pattern);
			} else {
				return this.optional(element) || value.match(/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/);
			}
		}, "Please enter a valid date.");
		
		/*自定义js函数验证
		 * <input type="text" name="xxx" customvalid="xxxFn(element)" title="xxx" />
		 */
		$.validator.addMethod("customvalid", function(value, element, params) {
			try{
				return eval('(' + params + ')');
			}catch(e){
				return false;
			}
		}, "Please fix this field.");
		
		$.validator.addClassRules({
			date: {date: true},
			alphanumeric: { alphanumeric: true },
			lettersonly: { lettersonly: true },
			phone: { phone: true },
			postcode: {postcode: true},
			chinaName: {chinaName: true}
		});
		$.validator.setDefaults({errorElement:"span"});
		$.validator.autoCreateRanges = true;
		
	}

})(jQuery);